package ru.nnstu.medialib.client;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.util.List;

import ru.nnstu.medialib.domain.Album;
import ru.nnstu.medialib.domain.Artist;
import ru.nnstu.medialib.domain.Song;
import ru.nnstu.medialib.server.Server;

public class Client {

	Server server = null;

	public void connectToServer() {
		try {
			server = (Server) Naming.lookup("//localhost/ServerImpl");
		} catch (Exception e) {
			System.err.println("RmiClient exception: " + e);
			e.printStackTrace();
		}
	}

	public List<Artist> listArtist() {
		try {
			return server.listArtist();
		} catch (Exception e) {
			System.err.println("RmiClient exception: " + e);
			e.printStackTrace();
			return null;
		}
	}

	public List<Album> listAlbum() {
		try {
			return server.listAlbum();
		} catch (Exception e) {
			System.err.println("RmiClient exception: " + e);
			e.printStackTrace();
			return null;
		}
	}

	public List<Song> listSong() {
		try {
			return server.listSong();
		} catch (Exception e) {
			System.err.println("RmiClient exception: " + e);
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		// Create and install a security manager
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new RMISecurityManager());
		}

		Client cli = new Client();
		// List<Artist> res = cli.listArtist();
		cli.connectToServer();
		List<Artist> res = cli.listArtist();
		for (Artist artist : res) {
			System.out.println("id: " + artist.getId() + " name: "
					+ artist.getName());
		}
		System.out.println("total : " + cli.listArtist().size());

	}

}
